These problems are offered to help understand the lecture material and assist with completing homeworks; they will also form the basis for some problems on the midterms. They are entirely optional, and feel free to skip if you understand a concept!
You should do these problems on paper or mentally first, and only THEN check the solution.
This set of problems uses the built-in Haskell notations for tuples and lists; for a version of the same problems using BB Haskell, with explicit definitions for Pairs and Lists see here.
2. BareBones Haskell: Polymorphic Types and Type Checking
Suppose we have the following data declarations:
data Either a b = Left a | Right b data Maybe a = Nothing | Just a
We assume that we can have Integer and Bools as defined in the Haskell Prelude. Assume for the purposes of these exercises that all arithmetic operators operate only on Integers, so pretend the type of (+) is:
(+): Integer -> Integer -> Integer.Therefore, all numeric types will simply be assumed to be Integer.
Give the type of the following (polymorphic) functions. Note: Assume that any numeric type is simply Integer. Also, use type variables starting with a, b, c, etc. There are no errors in these functions, all can be given a polymorphic type.
2.1.
id x = x
2.2.
select x y = x
2.3.
makePair x y = (x, y)
2.4.
makeSingleton x = [x]
2.5.
two x = (x, x)
2.6.
triple2Pair (x, y, z) = (x, (y, z))
2.7.
pair2List (x, y) = [x,y]
2.8.
list2Pairs [w,x,y,z] = ((w,x),(y,z))
2.9.
safeSecond (_:x:_) = Just x safeSecond _ = Nothing
2.10.
checkedSecond (_:x:_) = Right x checkedSecond _ = Left "Error: Empty List"
2.11.
weirdSecond (_:x:_) = Right (Just x) weirdSecond _ = Left Nothing
2.12.
strangeSecond (_:x:_) = Just (Right x) strangeSecond _ = Nothing
2.13.
bizarreHead [] = Nothing bizarreHead ((Just x):_) = Just (Just x)
2.14.
repeat1 x 0 = [] repeat1 x n = x : (repeat1 x (n-1))
2.15.
repeat2 x y 0 = [] repeat2 x y n = (x:y:(repeat2 x y (n-1)))
2.16.
repeatPairs x y 0 = [] repeatPairs x y n = ((x, y) : (repeatPairs x y (n-1)))
2.17.
try1 (x, y) z = (x, (y, z))
2.18.
try2 (x, y, z) = (x, (y z))
2.19.
try3 x y = x ( (<4) y )
2.20.
try4 x y z = x ((*3) y) ((`mod` z) 2)
2.21.
apply1 x y = x y
2.22.
apply2 x y = x (x y)
2.23.
apply3 x y z = (x y) z
2.24.
apply4 x y z = x (y z)
2.25.
apply5 x y z = x y z
2.26.
apply6 x y z = (x z) (y z)
2.27.
apply7 w x y z = w (x (y z))
2.28.
apply8 w x y z = ((w x) y) z
2.29.
apply9 w x y z = w x y z
2.30.
switch1 x y = switch1 y x -- will not terminate, but will type check!
2.31.
switch2 x y z = switch2 y x z
2.32.
switch3 x y z = switch3 z y x
2.33.
switch4 x y z = switch4 z x y
2.34.
lam1 = \x -> x
2.35.
lam2 = \x -> (x, x)
2.36.
lam3 = \x -> (x, 4, x)
2.37.
lam4 = \_ -> Nothing -- You can use an anonymous variable in lambda expression!
2.38.
lam5 = \x -> ((Left x), (Just x))
2.39.
lam6 = \x y -> y x
2.40.
lam7 = \x -> \y -> y x
2.41.
lam8 = \x -> \y -> f x y where f x = \x y -> (x, x)
2.42.
lam9 x = (\y -> x y)
2.43.
lam10 x y = (\z -> x (y, z))
2.44.
test1 = \x y -> ((\z -> x z) y)
2.45.
test2 x = \y -> ((\z -> x z) y)
2.46.
test3 x y = (x . y) 5
2.47.
test4 x y = ( (*3) . x) y
2.48.
test5 x y = (x . (*3)) y
2.49.
test6 = \x -> ((\y z -> y z) x 3)
2.50.
test7 x y = x . (`f` True) where f x y = (x, y)
End of Practice Problems 2 B.